package com.example.my_flea_street.controller;

import com.example.my_flea_street.bean.InfoPage;
import com.example.my_flea_street.bean.Notice;
import com.example.my_flea_street.bean.NoticePage;
import com.example.my_flea_street.bean.ResponseModel;
import com.example.my_flea_street.service.NoticeService;
import com.example.my_flea_street.service.impl.NoticeServiceImpl;
import com.example.my_flea_street.util.ResponseHelper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping(value = "/notice")
public class NoticeController {
    private final NoticeServiceImpl noticeService;

    @Autowired
    public NoticeController(NoticeServiceImpl noticeService) {
        this.noticeService = noticeService;
    }

    @RequestMapping(value = "/getNotices", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> searchInfo(@RequestBody Map<String, String> json ) {
        int page_no = Integer.parseInt(json.get("page_no"));
        int page_size = Integer.parseInt(json.get("page_size"));
        PageHelper.startPage(page_no,page_size);
        PageInfo<Notice> noticePage = new PageInfo<Notice>(noticeService.getNotices());
        NoticePage page = new NoticePage();
        page.setTotal((int)noticePage.getTotal());
        page.setPages(noticePage.getPages());
        page.setPageNum(noticePage.getPageNum());
        page.setData(noticePage.getList());
        return ResponseHelper.createResponse(0,"成功",page);
    }

    @RequestMapping(value = "/getNoticesTotal", method = {RequestMethod.POST, RequestMethod.GET})
    public int searchInfo() {
        return noticeService.getNoticesTotal();
    }
}
